//index.js
//获取应用实例
var app = getApp(), that
var Util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');

Page({
  data: {
    username: '',
    pwd: '',
    content: "",
    isload: false,
    result: [],
    hidden: true,
    disable: false,
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 调用应用实例的方法获取全局数据
    let app = getApp();
    that = this;
    // toast组件实例
    new app.ToastPannel();
    wx.getSystemInfo({
          
    })
  },
  
  // 触发toast组件
  openToastPannel: function () {
    if (this.data.content!=""){
      this.show(this.data.content);
    }
   
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
       
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  listenerinputusername: function (e) {
    this.data.username = e.detail.value
  },
  listnerinputpwd: function (e) {
    this.data.pwd = e.detail.value
  },
  saveuserinfo: function ($mobile, $appkey){
    var userinfos = { mobile: $mobile, appkey: $appkey }
    wx.setStorageSync('userinfos', userinfos);
    console.log("----保存缓存成功！-" + $mobile + $appkey);
  },
  listerlogin: function () {
    var that = this;
    console.log('账号为: ', this.data.username);
    var username = this.data.username;
    var pwd = this.data.pwd;
    if (username== "") {
      console.log("用户名位空！");
      that.openToastPannel();
      that.setData({
        content: "用户名位空"
      })
      return;
    }
    if (pwd=="") {
      console.log("密码为空！！！");
      that.openToastPannel();
      that.setData({
        content: "密码为空"
      })
      return;


    }
    console.log('密码为: ', this.data.pwd);
    if (username!="" && pwd!="") {
      // this.setData({
      //   hidden: false,
      //   disable: true
      // })
      console.log('密码为: ', that.data.pwd);
      console.log('账号: ', that.data.username);
      wx.showToast({
        title: '正在加载中.....',
        icon:'loading'
      })
      var myDate = new Date();  
      var thistime = myDate.getTime();   
      var appkey = app.globalData.appkey;
      var appsecret = app.globalData.appsecret;

      var str = that.data.username+appsecret+thistime;
      // var str = 'testtest1231507875913855';
      console.log('----------------'+str)

      var appsign = utilMd5.md5(str); 
      console.log(appsign)
      appsign = appsign.replace("+", "%2B");
      appsign = appsign.replace("=", "%2A");

      console.log(appsign +'-----------------------------appsign')

      console.log(appkey)
      console.log(thistime)
      console.log(that.data.username)
      wx.request({
       
        url: getApp().globalData.url,
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: Util.json2Form({
          mobile: that.data.username,
          password: that.data.pwd,
          viewtype: 1,
          appsign:appsign,
          date: thistime,
          appkey: appkey,
        }),
        success: function (res) {
          var result = res.data;
          console.log(res.data);
          console.log("----登录-----" + result.success);
          console.log("-----登录----" + result.errorMsg);
          if (result.success) {
            wx.hideLoading();
            that.saveuserinfo(that.data.username, result.errorMsg);
            wx.redirectTo({
              url: '../order/order?mobile=' + that.data.username + '&appkey=' + res.data.errorMsg,
              
            })
          }
          else{
            that.openToastPannel();
            that.setData({
              content: result.errorMsg
            })
            return;
          }
          // for (var j = 0; j < objs.length; j++) {
          //   console.log(objs[j].success);
          //   console.log(objs[j].errorMsg);
          // }
        },
        fail:res =>{
              console.log(res);
        }

      })


    }

  },
  
})
